package priv.liaohaolong.fullstacktest;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.jetbrains.annotations.NotNull;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableScheduling
@SpringBootApplication
@MapperScan("priv.liaohaolong.fullstacktest.dao")
public class FullStackTestApplication implements WebMvcConfigurer {

    @Override
    public void addViewControllers(@NotNull ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
        registry.addViewController("/login").setViewName("forward:/index.html");
        registry.addViewController("/about/**").setViewName("forward:/index.html");
        registry.addViewController("/forbidden").setViewName("forward:/index.html");
    }

    @Override
    public void addCorsMappings(@NotNull CorsRegistry registry) {
        // 配置 CORS
        registry.addMapping("/login") // 允许对 /login 路径的 POST 跨域请求
                .allowedOrigins("*") // 允许所有域
                .allowedMethods("POST");
        registry.addMapping("/captcha") // 允许对 /captcha 路径的 GET 跨域请求
                .allowedOrigins("*")
                .allowedMethods("GET");
        registry.addMapping("/api/**") // 允许对 /api/** 路径的跨域请求
                .allowedOrigins("*")
                .allowedMethods("*") // 允许所有方法
                .allowedHeaders("*"); // 允许所有请求头
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    public static void main(String[] args) {
        SpringApplication.run(FullStackTestApplication.class, args);
    }

}
